//
//  Labs_Singleton_DemoVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/4/10.
//

/**
 //相关链接：
 - **[Managing a Shared Resource Using a Singleton](https://developer.apple.com/documentation/swift/cocoa_design_patterns/managing_a_shared_resource_using_a_singleton)**  `官方文档`
 - **[Files and Initialization](https://developer.apple.com/swift/blog/?id=7)**  `官方博客`
 - **[Swift单例的正确写法](https://www.jianshu.com/p/10d6cc302366)**  `very good，有理有据`
 */


import UIKit

class Labs_Singleton_Manager {
    static let shared = Labs_Singleton_Manager()
}

class Labs_StandardSingleton_Manager {
    static let shared = Labs_StandardSingleton_Manager()
    private init() {}
}

class Labs_Singleton_DemoVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        test1()
    }
    
    private func test1() {
        var m1 = Labs_Singleton_Manager.shared
        var m2 = Labs_Singleton_Manager()
        var m3 = Labs_Singleton_Manager.init()
        
        print("\n m1 is ")
        printPointer(ptr: &m1)
        
        print("\n m2 is ")
        printPointer(ptr: &m2)
        
        print("\n m3 is ")
        printPointer(ptr: &m3)
        
        /*
         m1 is
        0x000000016b68c000

         m2 is
        0x000000016b68bff8

         m3 is
        0x000000016b68bff0
         */
    }
    
    private func test2() {
        var sm1 = Labs_StandardSingleton_Manager.shared
        /**
         'Labs_StandardSingleton_Manager' initializer is inaccessible due to 'private' protection level
         
         var sm2 = Labs_StandardSingleton_Manager()
         var sm3 = Labs_StandardSingleton_Manager.init()
         */
        
        print("\n sm1 is ")
        printPointer(ptr: &sm1)
    }
    
    func printPointer<T>(ptr: UnsafePointer<T>) {
        print(ptr)
    }

}
